package com.wuliu.web.controller.system;

import javax.servlet.http.HttpServletResponse;

import com.wuliu.common.core.domain.ApiResult;
import com.wuliu.common.core.domain.entity.BasCust;
import com.wuliu.common.utils.StringUtils;
import com.wuliu.common.utils.poi.ExcelUtil;
import com.wuliu.system.domain.dto.AddBasCustDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wuliu.common.annotation.Log;
import com.wuliu.common.core.controller.BaseController;
import com.wuliu.common.core.domain.AjaxResult;
import com.wuliu.common.enums.BusinessType;
import com.wuliu.system.service.IBasCustService;
import com.wuliu.common.core.page.TableDataInfo;

import java.util.List;

/**
 * 微信用户Controller
 *
 * @author realue
 * @date 2023-11-15
 */
@Api(tags = {"微信用户控制器"})
@RestController
@RequestMapping("/system/cust")
public class BasCustController extends BaseController {
    @Resource
    private IBasCustService basCustService;

    /**
     * 查询微信用户列表
     */
    @ApiOperation(value = "查询")
    @PreAuthorize("@ss.hasPermi('system:cust:list')")
    @GetMapping("/list")
    public TableDataInfo list(BasCust basCust) {
        basCust.setSysAccount(this.getSysAccount());
        startPage();
        List<BasCust> list = basCustService.selectBasCustList(basCust);
        return getDataTable(list);
    }

    /**
     * 导出微信用户列表
     */
    @ApiOperation(value = "导出")
    @PreAuthorize("@ss.hasPermi('system:cust:export')")
    @Log(title = "微信用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasCust basCust) {
        basCust.setSysAccount(this.getSysAccount());
        List<BasCust> list = basCustService.selectBasCustList(basCust);
        ExcelUtil<BasCust> util = new ExcelUtil<>(BasCust.class);
        util.exportExcel(response, list, "微信用户数据");
    }

    /**
     * 获取微信用户详细信息
     */
    @ApiOperation(value = "详细")
    @PreAuthorize("@ss.hasPermi('system:cust:query')")
    @GetMapping(value = "/{custNo}")
    public AjaxResult getInfo(@PathVariable("custNo") Integer custNo) {
        return success(basCustService.selectBasCustByCustNo(custNo));
    }

    /**
     * 新增微信用户
     */
    @ApiOperation(value = "新增")
    @PreAuthorize("@ss.hasPermi('system:cust:add')")
    @Log(title = "微信用户", businessType = BusinessType.INSERT)
    @PostMapping
    public ApiResult<Boolean> add(@RequestBody AddBasCustDto dto) {
        return ApiResult.success(basCustService.insertBasCust(dto));
    }

    /**
     * 修改微信用户
     */
    @ApiOperation(value = "编辑")
    @PreAuthorize("@ss.hasPermi('system:cust:edit')")
    @Log(title = "微信用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasCust basCust) {
        return toAjax(basCustService.updateBasCust(basCust));
    }

    /**
     * 删除微信用户
     */
    @ApiOperation(value = "删除")
    @PreAuthorize("@ss.hasPermi('system:cust:remove')")
    @Log(title = "微信用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{custNos}")
    public AjaxResult remove(@PathVariable Integer[] custNos) {
        return toAjax(basCustService.deleteBasCustByCustNos(custNos));
    }

    /**
     * 重置密码
     */
    @PreAuthorize("@ss.hasPermi('system:cust:resetPwd')")
    @Log(title = "微信用户", businessType = BusinessType.UPDATE)
    @PostMapping("/resetPwd")
    public ApiResult<Boolean> resetPwd(String newPwd, String custId) {
        if (StringUtils.isNotEmpty(newPwd)) {
            return ApiResult.error("新密码不能为空");
        }
        return ApiResult.success(basCustService.resetPwd(newPwd, custId));
    }
}
